import XRouter from '@/api/XRouter';

export default class XRouterConfig {

    private static xr: { [key: string]: string } = {};

    // {"index":"/pages/index/index"},
    public static instance(): XRouterConfig {
        return XRouterConfig.instanceObj;
    }

    private static instanceObj: XRouterConfig = new XRouterConfig();

    constructor() {
        XRouterConfig.init();
        return XRouterConfig.instanceObj;
    }

    static init() {
        const x: { [key: string]: string } = {};
        //首页
        x["home"] = "/pages/index/home";
        //用地面积
        x["data_use_area"] = "/pages/data/data_use_area_view";
        //办公区详情
        x["data_area_details"] = "/pages/data/data_area_details_view";
        //建筑面积
        x["data_building_area"] = "/pages/data/data_building_area_view";

        //上报管理
        x["data_post"] = "/pages/data/data_post_view";

        //配置/租用面积
        x["data_use_info"] = "/pages/data/data_use_info_view";

        //闲置面积
        x["data_idle_area"] = "/pages/data/data_idle_area_view";

        //单位闲置信息
        x["data_idle_unit_details"] = "/pages/data/data_idle_unit_details_view";

        //出租出售
        x["data_rent"] = "/pages/data/data_rent_view";

        //超标信息
        x["data_exceed"] = "/pages/data/data_exceed_view";
        //超标信息
        x["data_exceed_job_details"] = "/pages/data/data_exceed_job_details_view";
        //超标信息
        x["data_exceed_office_details"] = "/pages/data/data_exceed_office_details_view";
        //超标信息
        x["data_exceed_room_details"] = "/pages/data/data_exceed_room_details_view";

        //权属信息
        x["data_tenure"] = "/pages/data/data_tenure_view";
        x["data_tenure_details"] = "/pages/data/data_tenure_details_view";

        //登录
        x["login_view"] = "/pages/index/login_view";
        //房屋详情
        x["house_details"] = "/pages/house/house_details_view";
        //单位详情
        x["unit_details"] = "/pages/unit/unit_details_view";
        x["unit_view"] = "/pages/unit/unit_view";
		x["xunjian_list"] = "/pages/xunjian/list";
		x["xuncha_submit"] = "/pages/xunjian/submit";
		x["xuncha_detail"] = "/pages/xunjian/detail";
        //编制人数
        x["unit_user_details"] = "/pages/unit/unit_user_details_view";
        //楼栋数
        x["unit_house_details"] = "/pages/unit/unit_house_details_view";

        //超标面积
        x["unit_area_details"] = "/pages/unit/unit_area_details_view";
        //数据对比
        x["unit_data_ratio"] = "/pages/unit/unit_data_ratio_view";

        //楼层信息
        x["unit_house_level"] = "/pages/unit/unit_house_level_view";

        //楼层信息
        x["unit_house_level_img"] = "/pages/unit/unit_house_level_img_view";

        //通讯录
        x["user_contact"] = "/pages/user/user_contact_view";

        //核定计算器
        x["user_calculator"] = "/pages/user/user_calculator_view";

        //政策法规
        x["user_rule"] = "/pages/user/user_rule_view";
        //政策法规
        x["user_rule_details"] = "/pages/user/user_rule_details_view";

        //数据解锁
        x["user_data_open"] = "/pages/user/user_data_open_view";

        //修改密码
        x["user_update_psw"] = "/pages/user/user_update_psw_view";


        XRouterConfig.xr = x;
    }

    private static assembleParam(param?: { [key: string]: any }): string {
        if (!param) {
            return "";
        }
        let s = "";
        for (let k in param) {
            s += k + "=" + param[k] + "&";
        }
        if (s.endsWith("&")) {
            s = s.substring(0, s.length - 1);
        }
        return s;
    }

    public static async goTo(urlName: string, param?: { [key: string]: any }, complete?: any) {
        let p = XRouterConfig.xr[urlName];
        if (!p) {
            return;
        }
        const s = XRouterConfig.assembleParam(param);
        if (s && s.length > 0) {
            p += "?" + s;
        }
        XRouter.goTo(p, complete);
    }

    public static async redirect(urlName: string, param?: { [key: string]: any }) {
        let p = XRouterConfig.xr[urlName];
        if (!p) {
            return;
        }
        const s = XRouterConfig.assembleParam(param);
        if (s && s.length > 0) {
            p += "?" + s;
        }
        await XRouter.redirect(p);
    }

    public static pagePathByKey(key: string): string {
        const p = XRouterConfig.xr[key];
        if (!p) {
            return "";
        }
        return p;
    }

    public static replace(urlKey: string, param?: { [key: string]: any }) {
        XRouterConfig.redirect(urlKey, param);
    }

    public static back(nums: number) {
        XRouter.back(nums);
    }
}